// ============================================================================
//
// Copyright (C) 2006-2008 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.rcp.intro.actions;

import java.util.Arrays;

import org.dengues.rcp.i18n.Messages;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.preference.IPreferenceNode;
import org.eclipse.jface.preference.PreferenceDialog;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.dialogs.PreferencesUtil;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-8-6 qiang.zhang $
 * 
 */
public class OpenDenguesPreferencesAction extends Action implements ActionFactory.IWorkbenchAction {

    private IWorkbenchWindow workbenchWindow;

    public OpenDenguesPreferencesAction() {
        this(PlatformUI.getWorkbench().getActiveWorkbenchWindow());
    }

    public OpenDenguesPreferencesAction(IWorkbenchWindow window) {
        super(Messages.getString("ApplicationActionBarAdvisor.pref.text"));//$NON-NLS-1$
        if (window == null) {
            throw new IllegalArgumentException();
        }
        this.workbenchWindow = window;
        // @issue action id not set
        setToolTipText(Messages.getString("ApplicationActionBarAdvisor.pref.text"));//$NON-NLS-1$
        setId(getText());
    }

    /*
     * (non-Javadoc) Method declared on Action.
     */
    public void run() {
        if (workbenchWindow == null) {
            // action has been dispose
            return;
        }
        final String[] ids = new String[] { "org.dengues.prefs", "org.dengues.prefs.runtime", "org.dengues.core.prefs.codeColor",
                "org.dengues.ui.prefs.connectivity", "org.dengues.ui.prefs.connectivity.setting",
                "org.dengues.ui.prefs.etl.componentSettings", "org.dengues.ui.prefs.designer.cdc.settingsPage" };
        PreferenceDialog dialog = PreferencesUtil.createPreferenceDialogOn(null, null, null, null);
        dialog.getTreeViewer().addFilter(new ViewerFilter() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.jface.viewers.ViewerFilter#select(org.eclipse.jface.viewers.Viewer, java.lang.Object,
             * java.lang.Object)
             */
            @Override
            public boolean select(Viewer viewer, Object parentElement, Object element) {
                if (element instanceof IPreferenceNode) {
                    IPreferenceNode node = (IPreferenceNode) element;
                    return Arrays.asList(ids).contains(node.getId());
                }
                return false;
            }
        });
        dialog.open();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.actions.ActionFactory.IWorkbenchAction#dispose()
     */
    public void dispose() {
        workbenchWindow = null;
    }

}
